using System;
using System.Collections.Generic;
using System.Text;
using Mpi.Common.Collections;
using microkernel;
using Xeml.Document.Contracts;
using Xeml.Document;

namespace Xeml.Manager
{
    
    public sealed class ValidatorManager
    {
        #region Singleton pattern
        static readonly ValidatorManager instance = new ValidatorManager();

        static ValidatorManager()
        {
        }

        ValidatorManager()
        {
            //parameterValidatorComponents = new IdentifiableObjectDictionary<Guid, ComponentInfoObject>();
            //validatorComponents = new IdentifiableObjectDictionary<Guid, ComponentInfoObject>();
            _validationComponents = new Dictionary<string, IValidator>();
            Init();

        }

        public static ValidatorManager Instance
        {
            get
            {
                return instance;
            }
        } 
        #endregion

        #region Fields
        //private IdentifiableObjectDictionary<Guid, ComponentInfoObject> parameterValidatorComponents;
        //private IdentifiableObjectDictionary<Guid, ComponentInfoObject> validatorComponents; 
        private Dictionary<string, IValidator> _validationComponents;
        #endregion

        #region Properties
        ///// <summary>
        ///// A collection of available parameter rssource components.
        ///// </summary>
        //public IdentifiableObjectDictionary<Guid, ComponentInfoObject> ParameterValidatorComponents
        //{
        //    get
        //    {
        //        return this.parameterValidatorComponents;
        //    }
        //}

        ///// <summary>
        ///// A collection of available validator components.
        ///// </summary>
        //public IdentifiableObjectDictionary<Guid, ComponentInfoObject> ValidatorComponents
        //{
        //    get
        //    {
        //        return this.validatorComponents;
        //    }
        //}

        /// <summary>
        /// A collection of available validator components.
        /// </summary>
        public Dictionary<string, IValidator> ValidationComponents
        {
            get
            {
                return _validationComponents;
            }
        } 
        #endregion

        #region Methods

        /// <summary>
        /// Initialize the manager. All available components will be loaded and component information will be readed.
        /// </summary>
        private void Init()
        {

            TypeReference[] validatorTypeList = ObjectFactory.TypeList(typeof(IValidator));
           // TypeReference[] paramValidatorTypeList = ObjectFactory.TypeList(typeof(IParameterValidator));
            for (int i = 0; i < validatorTypeList.Length; i++)
            {
                IValidator v = (IValidator)ObjectFactory.CreateInstance(typeof(IValidator), i);
                //ComponentInfoObject vci = new ComponentInfoObject(pr, i);
                _validationComponents.Add(v.Uri,v);
               // this.parameterValidatorComponents.Add(vci);
            }

            //for (int i = 0; i < validatorTypeList.Length; i++)
            //{
            //    IValidator pr = (IValidator)ObjectFactory.CreateInstance(typeof(IValidator), i);
            //    ComponentInfoObject vci = new ComponentInfoObject(pr, i);
            //    this.validatorComponents.Add(vci);
            //}

            XemlCoreValidator xcv = new XemlCoreValidator();
            //ComponentInfoObject cio = new ComponentInfoObject(xcv, -1);
            this._validationComponents.Add(xcv.Uri,xcv);


        }


        /// <summary>
        /// Craeates an instance of a validator component.
        /// </summary>
        /// <param name="compId">The component identifiere.</param>
        /// <returns>A vaildator object.</returns>
        /// <exception cref="Xeml.Component.XemlComponentLoadFailed">Will be throwed if the component can'delayTimer be found.</exception>
        public IValidator CreateValidator(string uri)
        {
            if (_validationComponents.ContainsKey(uri))
            {
                try
                {
                    IValidator ret;
                    ret = (IValidator) _validationComponents[uri].Copy();
                    return ret;
                }
                catch (Exception ex)
                {
                    throw new XemlComponentLoadFailedException(uri, "Handler exist, but loading failed", ex);
                }
                //if (compId == new Guid("http://www.mpimp-golm.mpg.de/2007/XEML/CoreValidator"))
                //    ret = new XemlCoreValidator();
                //else
                //    ret = (IValidator)ObjectFactory.CreateInstance(typeof(IValidator), validatorComponents[compId].Index);
                //return ret;
            }
            else
            {
                throw new XemlComponentLoadFailedException(uri, "Component not found.");
            }

        }


        ///// <summary>
        ///// Creates an instance of a parameter resource.
        ///// </summary>
        ///// <param name="compId">The component identifiere.</param>
        ///// <param name="nameSpace">The name space for the resource.</param>
        ///// <returns>A parameter ressource object.</returns>
        ///// <exception cref="Xeml.Component.XemlComponentLoadFailed">Will be throwed if the component can't be found.</exception>
        //public IParameterValidator CreateParameterValidator(Guid compId, string nameSpace)
        //{
        //    if (this.parameterValidatorComponents.Contains(compId))
        //    {
        //        IParameterValidator ret = null;
        //        try
        //        {
        //            ret = (IParameterValidator)ObjectFactory.CreateInstance(typeof(IParameterValidator), parameterValidatorComponents[compId].Index);
        //        }
        //        catch (Exception ex)
        //        {
        //            throw new XemlComponentLoadFailedException(compId, "Component exist, but loading failed", ex);
        //        }
        //        ret.NameSpace = nameSpace;
        //        return ret;
        //    }
        //    else
        //        throw new XemlComponentLoadFailedException(compId, "Component not found.");

        //}

        ///// <summary>
        ///// Creates an insatnce of a parameter ressource.
        ///// </summary>
        ///// <param name="compId">The component identifiere.</param>
        ///// <param name="nameSpace">The name space for theressource.</param>
        ///// <param name="location">The location of the parameter definitions.</param>
        ///// <returns>A parameter ressource object.</returns>
        //public IParameterValidator CreateParameterValidator(Guid compId, string nameSpace, string location)
        //{
        //    IParameterValidator ret;
        //    ret = CreateParameterValidator(compId, nameSpace);
        //    ret.ResourceLocation = location;
        //    return ret;
        //}

        //public void EnrichParamValResource(ParameterValidationResource pvr)
        //{
        //    try
        //    {
        //        if (this.parameterValidatorComponents.Contains(pvr.ComponentId))
        //        {
        //            IParameterValidator v = CreateParameterValidator(pvr.ComponentId, pvr.NameSpace);
        //            pvr.Validator = v;
        //            pvr.ComponentState = ComponentLoadState.Loaded;
        //        }
        //        else
        //            pvr.ComponentState = ComponentLoadState.UnknownSample;
        //    }
        //    catch (XemlComponentLoadFailedException ex)
        //    {
        //        pvr.ComponentState = ComponentLoadState.Failed;
        //    }
        //}

        #endregion
    }
}
